package com.example.demo.web.rest.controller;

import com.example.demo.client.evt.sysbasequery.QueryCodeListEvt;
import com.example.demo.client.evt.sysbasequery.QueryTrusteeshipEvt;
import com.example.demo.client.evt.sysbasequery.UserIdEvt;
import com.example.demo.client.vm.sysbasequery.QueryUserTrusteeshipVm;
import com.ffcs.oss.param.out.ServiceResp;
import com.ffcs.oss.param.vm.QueryPageVm;
import com.example.demo.service.UserTrusteeshipService;
import com.example.demo.web.rest.evt.IdEvt;
import com.example.demo.web.rest.evt.SaveUserTrusteeshipEvt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户托管管理
 * @Author zhengmf
 * @Date 2019/1/3  18:13
 */
@Api("用户托管管理")
@RestController
@RequestMapping("/api/trusteeship")
public class UserTrusteeshipController extends BaseController{
    @Autowired
    private UserTrusteeshipService userTrusteeshipService;

    @ApiOperation("获取托管静态数据")
    @PostMapping(value = "/getTrusteeshipStaticData")
    public ServiceResp<Map> getTrusteeshipStaticData(){
        Map<String,Object> map = new HashMap<>();
        map.put("currentUserId",userTrusteeshipService.getCurrentUserId());
        QueryCodeListEvt evt = new QueryCodeListEvt();
        evt.setTableName("AAS_USER_TRUSTEESHIP_TBL");
        evt.setColName("TRUS_TYPE");
        map.put("trusteeshipType",baseQueryClient.getByColNameAndTableName(evt).getBody());
        return ServiceResp.getInstance(Map.class).success(map);
    }

    @ApiOperation("查询托管信息")
    @PostMapping(value = "/queryTrusteeship")
    public ServiceResp<QueryPageVm<QueryUserTrusteeshipVm>> queryTrusteeship(@RequestBody QueryTrusteeshipEvt evt){
        return baseQueryClient.queryTrusteeship(evt);
    }

    @ApiOperation("保存托管信息")
    @PostMapping(value = "/saveUserTrusteeship")
    public ServiceResp<Boolean> saveUserTrusteeship(@RequestBody @Validated SaveUserTrusteeshipEvt evt){
        //被重复托管人id
        Long userTrusId = userTrusteeshipService.saveUserTrusteeship(evt);
        if(userTrusId != null){
            UserIdEvt userIdEvt = new UserIdEvt();
            userIdEvt.setUserId(userTrusId);
            return ServiceResp.getInstance(Boolean.class)
                .success(false,baseQueryClient.getUserById(userIdEvt).getBody().getAlias());//已托管过，不能重复托管
        }
        return ServiceResp.getInstance(Boolean.class).success(true,"用户托管保存成功");
    }

    @ApiOperation("删除托管信息，逻辑删除")
    @PostMapping(value = "/delUserTrusteeship")
    public ServiceResp<Boolean> delUserTrusteeship(@RequestBody IdEvt evt){
        userTrusteeshipService.updateStatus(evt.getId());
        return ServiceResp.getInstance(Boolean.class).success(true);
    }

}
